//
//  InvestmentBaseVC.m
//  ZZLC
//
//  Created by 杨春至 on 2017/12/16.
//  Copyright © 2017年 ZhongZeng. All rights reserved.
//

#import "InvestmentBaseVC.h"
#import "MyInvestmentTableViewCell.h"
#import "InVestModel.h"
#import "ContactContetnVC.h"
#import "MMMDetailVC.h"

@interface InvestmentBaseVC ()<UITableViewDataSource,UITableViewDelegate,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

@property(nonatomic)NSMutableArray *dataSource;
@property (nonatomic,assign) NSInteger currentPage;
@end

@implementation InvestmentBaseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configureTableView];
    _currentPage = 1;
    [self loadData];
    // Do any additional setup after loading the view from its nib.
}
- (void)configureTableView
{
    _tableView.emptyDataSetSource = self;
    _tableView.emptyDataSetDelegate = self;
    _tableView.backgroundColor = RGBCOLOR(245, 245, 245);
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _currentPage = 1;
        [self loadData];
    }];
    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _currentPage += 1;
        [self loadData];
    }];
}
- (void)loadData
{
    [NetWorkingManager postWithRequestUrl:@"User/loadInvestorsRecordNew" AndParameters:@{@"parameters":[@{@"currentPage":@(_currentPage),@"type":[NSString stringWithFormat:@"%ld",(long)self.categoryNum],@"authorization":[UserModel authorization]?[UserModel authorization]:@""} DataTOjsonString]} AndRequestBlock:^(NSDictionary *responseObject) {
        if (_currentPage == 1) {
            self.dataSource = [InVestModel mj_objectArrayWithKeyValuesArray:[responseObject objectForKey:@"list"]];
        }else{
            [self.dataSource addObjectsFromArray: [InVestModel mj_objectArrayWithKeyValuesArray:[responseObject objectForKey:@"list"]]];
        }
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
        
    } FailBlock:^(NSDictionary *errDic) {
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
    }];


}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"MyInvestmentTableViewCell" owner:self options:nil];
    MyInvestmentTableViewCell *cell = [nibs firstObject];
    InVestModel *model = self.dataSource[indexPath.row];
    [cell configureCellByInvestModel:model];
    cell.titleLabel.preferredMaxLayoutWidth = SCREEN_WIDTH-180;
    [cell.contactURLBtn bk_whenTapped:^{
        ContactContetnVC *contactVC = [[ContactContetnVC alloc]init];
        contactVC.urlStr = model.contractURL;
        [self.navigationController pushViewController:contactVC animated:YES];
    }];
    [cell.productIDBtn bk_whenTapped:^{
        MMMDetailVC *dVC = [[MMMDetailVC alloc]init];
        dVC.pId = model.productId;
        [self.navigationController pushViewController:dVC animated:YES];
    }];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 270;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
